// pages/appointment-success/appointment-success.js
Page({
	data: {
		appointmentInfo: {
			doctorName: '',
			doctorTitle: '',
			appointmentTime: '',
			waitingTime: '',
			department: '',
			hospital: '',
			patientName: '',
			fee: ''
		},
		selectedCampus: '湘南院区' // 添加院区信息
	},

	onLoad(options) {
		console.log('预约成功页面加载', options)
		
		// 接收传递的预约信息
		if (options.doctor && options.date && options.time) {
			this.setData({
				selectedCampus: options.campus || '湘南院区' // 接收院区信息
			})
			this.generateAppointmentInfo(options)
		}
	},

	// 生成预约信息
	generateAppointmentInfo(options) {
		// 根据医生姓名生成不同的医院信息
		const doctorHash = this.hashCode(options.doctor)
		const hospitalInfo = this.generateHospitalInfo(doctorHash)
		const patientInfo = this.generatePatientInfo(doctorHash)
		
		// 根据院区生成医院名称
		const hospitalName = `惠仁医院-${this.data.selectedCampus}`
		
		this.setData({
			appointmentInfo: {
				doctorName: options.doctor,
				doctorTitle: options.title || '主任医师',
				appointmentTime: options.date,
				waitingTime: options.time,
				department: options.department || '',
				hospital: hospitalName,
				patientName: patientInfo.name,
				fee: options.fee || '60'
			}
		})
	},

	// 生成医院信息
	generateHospitalInfo(hash) {
		const hospitals = [
			{
				hospital: '惠仁医院-东院区',
				        address: '北京市大兴区惠仁路990号'
			},
			{
				hospital: '惠仁医院-西院区',
				        address: '北京市朝阳区惠仁路123号'
			},
			{
				hospital: '惠仁医院-北院区',
				        address: '北京市海淀区惠仁路456号'
			}
		]
		
		const hospitalIndex = hash % hospitals.length
		return hospitals[hospitalIndex]
	},

	// 生成患者信息
	generatePatientInfo(hash) {
		const patients = [
			{ name: '牛韦', idNumber: '610*********0989' },
			{ name: '张三', idNumber: '110*********1234' },
			{ name: '李四', idNumber: '320*********5678' },
			{ name: '王五', idNumber: '440*********9012' }
		]
		
		const patientIndex = hash % patients.length
		return patients[patientIndex]
	},

	// 简单的哈希函数
	hashCode(str) {
		let hash = 0
		if (str.length === 0) return hash
		for (let i = 0; i < str.length; i++) {
			const char = str.charCodeAt(i)
			hash = ((hash << 5) - hash) + char
			hash = hash & hash
		}
		return Math.abs(hash)
	},

	// 返回上一页
	goBack() {
		wx.navigateBack()
	},

	// 查看预约详情
	viewAppointmentDetails() {
		console.log('查看预约详情')
		
		// 跳转到预约详情页面
		wx.navigateTo({
			url: `/pages/appointment-detail/appointment-detail?doctor=${this.data.appointmentInfo.doctorName}&date=${this.data.appointmentInfo.appointmentTime}&time=${this.data.appointmentInfo.waitingTime}&department=${this.data.appointmentInfo.department}&title=${this.data.appointmentInfo.doctorTitle}&fee=${this.data.appointmentInfo.fee}&hospital=${this.data.appointmentInfo.hospital}&patientName=${this.data.appointmentInfo.patientName}&campus=${this.data.selectedCampus}`
		})
	},

	// 返回首页
	goHome() {
		console.log('返回首页')
		wx.reLaunch({
			url: '/pages/index/index'
		})
	},

	onShow() {
		console.log('预约成功页面显示')
	}
})
